﻿using System;

using HsTec.Mobile.Green.BLL;
using HsTec.Mobile.Green.Model;

public partial class PowerWarning_ChangeRateInfoB_Edit : System.Web.UI.Page
{
    private string sEditStatus = "1";
    private ChangeRateInfoBLL bll = new ChangeRateInfoBLL();
    private string sID = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_Pers_ID"] == null)
        {
            Response.Redirect("../Logon.aspx");
        }
        try
        {
            sID = this.Request.QueryString["ID"].ToString().Trim();
        }
        catch
        { }
        if (!IsPostBack)
        {
            sltAreabind();
            try
            {
                sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();

                if (sEditStatus == "2")
                {
                    ChangeRateInfo model = bll.GetModel(int.Parse(sID));
                    if (model != null)
                    {
                        txtEnergyChange.Text = model.EnergyChange.ToString();
                        txtBaseChange.Text = model.BaseChange.ToString();
                        txtTrafficChange.Text = model.TrafficChange.ToString();
                        sltYear.Value = model.Years;
                        sltArea.Text = model.Area_ID.ToString();
                    }
                }
                else
                {
                    sltYear.Value = DateTime.Now.Year.ToString();
                }
            }
            catch
            {

            }
        }
    }
    private void sltAreabind()
    {
        sltArea.DataTextField = "Name";
        sltArea.DataValueField = "ID";
        sltArea.DataSource = bll.GetAreaList();
        sltArea.DataBind();
        sltArea.Items.Insert(0, "请选择...");
        sltArea.Items[0].Value = "";
    }
    protected void SaveData_ServerClick(object sender, EventArgs e)
    {
        ChangeRateInfo model = new ChangeRateInfo();
        try
        {
            model.Years = sltYear.Value;
            model.Area_ID = int.Parse(sltArea.SelectedValue.Trim());
            model.EnergyChange = Decimal.Parse(txtEnergyChange.Text.ToString().Trim());
            model.BaseChange = Decimal.Parse(txtBaseChange.Text.ToString().Trim());
            model.TrafficChange = Decimal.Parse(txtTrafficChange.Text.ToString().Trim());
            model.Type = "2";
            sEditStatus = this.Request.QueryString["EditStatus"].ToString().Trim();
            if (sEditStatus == "1")
            {
                if (bll.Exists(" Years='" + model.Years + "' and Area_ID='" + model.Area_ID + "'"))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + model.Years + "年数据已经存在！');</script>");
                    return;
                }
                int iback = bll.Add(model);
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存成功!');window.close();</script>");
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('保存失败!');</script>");
                    return;
                }
            }
            else
            {
                model.CRI_ID = this.Request.QueryString["ID"].ToString().Trim() == "" ? 0 : int.Parse(this.Request.QueryString["ID"].ToString().Trim());
                if (bll.Exists(" Years='" + model.Years + "'and Area_ID='" + model.Area_ID + "' and CRI_ID<>'" + model.CRI_ID + "'"))
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + model.Years + "年数据已经存在！');</script>");
                    return;
                }
                int iback = bll.Update(model);
                if (iback > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改成功!');window.close();</script>");  //window.opener.location.reload();
                    return;
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('修改失败!');</script>");
                    return;
                }
            }
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('操作失败!');</script>");
            return;
        }
    }
}
